package com.taptech.doufu.ui.viewholder;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.taptech.doufu.ui.activity.UserDetailActivity;
import com.taptech.doufu.util.StringUtil;
import com.taptech.doufu.R;
import com.taptech.doufu.ui.activity.NewOthersPersonalActivity;
import com.taptech.doufu.app.WeMediaApplication;
import com.taptech.doufu.bean.MineAbstractBean;
import com.taptech.doufu.bean.UserBean;
import com.taptech.doufu.bean.novel.NovelTyrantBean;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.ui.view.drawcircle.BaseRecyclerViewViewHolder;
import com.taptech.doufu.services.personalcenter.AccountService;
import com.taptech.doufu.ui.activity.PersonalDynamicActivity;
import com.taptech.doufu.util.DiaobaoUtil;
import com.taptech.doufu.util.ImageManager;
import com.taptech.doufu.ui.view.RoundImageView;

/**
 * Created by ${LiYi} on 2017/1/5.
 */

public class NovelTyrantRankViewHolder <T extends NovelTyrantBean> extends BaseRecyclerViewViewHolder implements View.OnClickListener {
    private TextView rankTv,authorNameTv,userSumTv;

    RoundImageView userImg,authorMedalImg;
    ImageView crownImg;
    NovelTyrantBean novelTyrantBean;

    public NovelTyrantRankViewHolder(Context context, View itemView) {
        super(context, itemView);
        bindView();
    }

    private void bindView(){
        rankTv = (TextView) itemView.findViewById(R.id.novel_tyrant_rank_item_rank);
        authorNameTv = (TextView) itemView.findViewById(R.id.novel_tyrant_rank_item_author_name);
        userSumTv = (TextView) itemView.findViewById(R.id.novel_tyrant_rank_item_author_sum);

        userImg = (RoundImageView) itemView.findViewById(R.id.novel_tyrant_rank_item_icon);
        authorMedalImg = (RoundImageView) itemView.findViewById(R.id.novel_tyrant_rank_item_author_medal);
        crownImg = (ImageView) itemView.findViewById(R.id.novel_tyrant_rank_item_crown);
        itemView.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        if(novelTyrantBean == null || novelTyrantBean.getUser() == null){
            return;
        }
        if (!novelTyrantBean.getUser().getUserId().equals(AccountService.getInstance().getUserUid())){
            UserDetailActivity.Companion.startActivity(mContext,novelTyrantBean.getUser().getUserId());
        } else {
            Intent intent = new Intent(mContext, PersonalDynamicActivity.class);
            mContext.startActivity(intent);
        }
    }

    @Override
    public void setViewHolderChildViewContent(MineAbstractBean bean, int position) {
        if(bean == null){
            return;
        }

        if(position < 3){
            rankTv.setTextColor(getRoseColor());
            crownImg.setVisibility(View.VISIBLE);
            setCrownImgBg(position);
        }else {
            rankTv.setTextColor(getBlackColor());
            crownImg.setVisibility(View.GONE);
        }

        novelTyrantBean = (NovelTyrantBean) bean;
        rankTv.setText(novelTyrantBean.getRank());
        authorNameTv.setText(novelTyrantBean.getUser().getName());
        dealSumTextColor(novelTyrantBean.getSum());
        UserBean userBean = novelTyrantBean.getUser();
        Drawable userBitmap = mContext.getResources().getDrawable(R.drawable.default_user_portrait);
        userImg.setImageDrawable(userBitmap);
        if (userBean != null) {
            if (userBean.getIcon() != null) {
                ImageManager.displayImage(userImg, userBean.getIcon(), 0);
            }
            DiaobaoUtil.setMedalImgView(userBean.getMedal(), authorMedalImg);
        }
    }

    private void dealSumTextColor(String sum){
        if(StringUtil.stringLength(sum) && sum.length() > 4 ){
            SpannableStringBuilder builder = new SpannableStringBuilder(sum);
            ForegroundColorSpan span = new ForegroundColorSpan(getRoseColor());
            builder.setSpan(span, 2, sum.length()- 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            userSumTv.setText(builder);
            return;
        }
        userSumTv.setText(sum);
    }
    private int  getRoseColor(){
        int color ;
        if(WeMediaApplication.getInstance().isDayNightMode_Night){
            color =  mContext.getResources().getColor(R.color.theme_main_dark);
        }else {
            color =  mContext.getResources().getColor(R.color.text_color_17);
        }
        return color;
    }

    private int  getBlackColor(){
        int color ;
        if(WeMediaApplication.getInstance().isDayNightMode_Night){
            color =  mContext.getResources().getColor(R.color.text_dark);
        }else {
            color =  mContext.getResources().getColor(R.color.text_color_1);
        }
        return color;
    }

    private  void setCrownImgBg(int position){
        if(position > 2){
            return;
        }
        int drawable = 0;
        switch (position){
            case 0:
                 drawable =R.drawable.cp_rank_first;
                break;
            case 1:
                drawable =R.drawable.cp_rank_second;
                break;
            case 2:
                drawable =R.drawable.cp_rank_third;
                break;
        }
        crownImg.setImageResource(drawable);
    }

}
